package org.jsola.contract.provider.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.contract.provider.IMessageProviderService;
import org.jsola.message.dto.ShortMessageDTO;
import org.jsola.message.dto.WeiXinDTO;
import org.jsola.message.service.ISmsSendService;
import org.jsola.message.service.IWeiXinSendService;
import org.jsola.message.vo.SmsSendLogVO;
import org.jsola.message.vo.WeixinTempSendLogVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author zxy
 */
@Slf4j
@Service("contractMessageProviderServiceImpl")
public class MessageProviderServiceImpl implements IMessageProviderService {

    @Autowired
    private ISmsSendService sendShortMessage;

    @Autowired
    private IWeiXinSendService weiXinSendService;

    @Value("${spring.application.name}")
    private String appName;


    @Override
    public boolean sendShortMessage(ShortMessageDTO shortMessageDTO, TokenUser tokenUser) {
        // 平台
        shortMessageDTO.setAppName(appName);

        SmsSendLogVO smsSendLogVO = sendShortMessage.sendShortMessage(shortMessageDTO, tokenUser);
        return Objects.nonNull(smsSendLogVO);
    }

    @Override
    public boolean verifyCaptcha(ShortMessageDTO shortMessageDTO, TokenUser tokenUser) {
        return sendShortMessage.verifyCaptcha(shortMessageDTO, tokenUser);
    }

    @Override
    public void sendWeChatTempMessage(WeiXinDTO weiXinDTO, TokenUser tokenUser) {
        try {
            weiXinSendService.sendWeiXinTempMessage(weiXinDTO, tokenUser);
        } catch (Exception e) {
            log.error("发送公众号模板消息失败,错误信息:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public List<WeixinTempSendLogVO> batchSendWeChatTempMessage(WeiXinDTO weiXinDTO, TokenUser tokenUser){
        return weiXinSendService.batchSendWeiXinTempMessage(weiXinDTO, tokenUser);
    }
}
